package cj.com.chabaike.ui.frag;

import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cj.com.chabaike.R;
import cj.com.chabaike.ui.act.MainPager;
import cj.com.chabaike.ui.utils.TransitionHelper;

/**
 * Created by CJLAB on 2015/8/8.
 * 用于实现卡片式动画效果的覆盖
 */
public class MainFragment extends TransitionHelper.BaseFragment implements RecyclerFragment1.OnFragmentInteractionListener {
    List<Fragment> fragList ;
    String[] title = new String[]{"首页","专题", "新闻","奇趣","体娱","健康美食", "功夫厨房","健康","旅游" } ;
    @Override
    public void onFragmentInteraction(Uri uri) { //暂时没什么用

    }



    class MyFragmentAdpaer extends FragmentPagerAdapter {

        private FragmentManager manager;
        public MyFragmentAdpaer(FragmentManager fm) {
            super(fm);
            this.manager=fm;
        }

        @Override
        public android.support.v4.app.Fragment getItem(int position) {
            manager.beginTransaction().show(fragList.get(position));
            return fragList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            manager.beginTransaction().hide(fragList.get(position));
        }

        @Override
        public int getCount() {

            return fragList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {

            return title[position];
        }



    }
    public TabLayout tbl;
    public ViewPager vp;


    /*******************本Fragment的生命周期方法***********************/
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mainviewpager, null, false);
        tbl = (TabLayout)view.findViewById(R.id.tablayout);
        vp = (ViewPager) view.findViewById(R.id.vp);
        initFragList();
        inittb();
        return view;

    }





/*******************初始化代码********************************/

private void initFragList() {
    fragList = new ArrayList<>();
    RecyclerFragment1 rf1 = new RecyclerFragment1(); //首页
    Recycler_Special_Fragment special_fragment = new Recycler_Special_Fragment();

    Recycler_NewsType_Fragment rf2 = new Recycler_NewsType_Fragment();
    Bundle bundle_rf2 = new Bundle();
    bundle_rf2.putInt("tag_type", 0);
    rf2.setArguments(bundle_rf2);


    Recycler_NewsType_Fragment rf3 = new Recycler_NewsType_Fragment();




    Recycler_NewsType_Fragment rf4 = new Recycler_NewsType_Fragment();
    Recycler_NewsType_Fragment rf5 = new Recycler_NewsType_Fragment();
    Recycler_NewsType_Fragment rf6 = new Recycler_NewsType_Fragment();
    Recycler_NewsType_Fragment rf7 = new Recycler_NewsType_Fragment();
    Recycler_NewsType_Fragment rf8 = new Recycler_NewsType_Fragment();
    Bundle bundle_rf3 = new Bundle();
    bundle_rf3.putInt("tag_type", 1);
    rf3.setArguments(bundle_rf3);

    Bundle bundle_rf4 = new Bundle();
    bundle_rf4.putInt("tag_type", 2);
    rf4.setArguments(bundle_rf4);

    Bundle bundle_rf5 = new Bundle();
    bundle_rf5.putInt("tag_type", 3);
    rf5.setArguments(bundle_rf5);

    Bundle bundle_rf6 = new Bundle();
    bundle_rf6.putInt("tag_type", 4);
    rf6.setArguments(bundle_rf6);

    Bundle bundle_rf7 = new Bundle();
    bundle_rf7.putInt("tag_type", 5);
    rf7.setArguments(bundle_rf7);

    Bundle bundle_rf8 = new Bundle();
    bundle_rf8.putInt("tag_type", 6);
    rf8.setArguments(bundle_rf8);

    fragList.add(rf1);
    fragList.add(special_fragment);
    fragList.add(rf2);
    fragList.add(rf3);
    fragList.add(rf4);
    fragList.add(rf5);
    fragList.add(rf6);
    fragList.add(rf7);
    fragList.add(rf8);
}


    private void inittb() {

        tbl.setTabTextColors(Color.BLACK, Color.WHITE);

        TabLayout.Tab tab1 = tbl.newTab();

        tbl.setTabTextColors(Color.WHITE,Color.RED);
        tbl.addTab(tab1);
        tbl.addTab(tab1);
        tbl.addTab(tab1);
        tbl.addTab(tab1);
        tbl.addTab(tab1);
        tbl.addTab(tab1);
        MyFragmentAdpaer myFragmentAdpaer = new MyFragmentAdpaer(MainPager.of(getActivity()).getSupportFragmentManager());


        vp.setAdapter(myFragmentAdpaer);
        tbl.setupWithViewPager(vp);
        tbl.setTabsFromPagerAdapter(myFragmentAdpaer);
    }


}




